package com.sys.ai.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sys.ai.domain.AiEmbeddingDb;
import com.sys.ai.domain.AiKnowledge;
import com.sys.ai.mapper.AiKnowledgeMapper;
import com.sys.ai.service.*;
import com.sys.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;

/**
 * 知识库Service业务层处理
 *
 * @author LGY
 * @date 2025-05-26
 */
@Slf4j
@Service
public class AiKnowledgeServiceImpl extends ServiceImpl<AiKnowledgeMapper, AiKnowledge> implements IAiKnowledgeService {
    @Autowired
    private AiKnowledgeMapper aiKnowledgeMapper;
    @Autowired
    @Lazy
    private IDbOptionService dbOptionService;
    @Autowired
    @Lazy
    private IAiKnowledgeFragmentationService aiKnowledgeFragmentationService;
    @Autowired
    @Lazy
    private IAiKnowledgeFileService aiKnowledgeFileService;
    @Autowired
    @Lazy
    private IAiEmbeddingDbService aiEmbeddingDbService;

    /**
     * 查询知识库
     *
     * @param id 知识库主键
     * @return 知识库
     */
    @Override
    public AiKnowledge selectAiKnowledgeById(String id) {
        return this.getById(id);
    }

    /**
     * 查询知识库列表
     *
     * @param aiKnowledge 知识库
     * @return 知识库
     */
    @Override
    public List<AiKnowledge> selectAiKnowledgeList(AiKnowledge aiKnowledge) {
        return aiKnowledgeMapper.selectAiKnowledgeList(aiKnowledge);
    }

    /**
     * 新增知识库
     *
     * @param aiKnowledge 知识库
     * @return 结果
     */

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void insertAiKnowledge(AiKnowledge aiKnowledge) {
        this.save(aiKnowledge);
        // 更新向量数据库class名称
        String username = SecurityUtils.getUsername();
        aiKnowledge.setVectorDb(username.toUpperCase() + aiKnowledge.getId());
        // 创建向量数据库
        AiEmbeddingDb aiEmbeddingDb = aiEmbeddingDbService.getById(aiKnowledge.getEmbeddingDbId());
        Assert.notNull(aiEmbeddingDb, "向量数据库不存在");
        dbOptionService.createAiKnowledge(aiKnowledge.getVectorDb(), aiEmbeddingDb);
        this.updateById(aiKnowledge);
    }

    /**
     * 修改知识库
     *
     * @param aiKnowledge 知识库
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateAiKnowledge(AiKnowledge aiKnowledge) {
        this.updateById(aiKnowledge);
    }

    /**
     * 批量删除知识库
     *
     * @param ids 需要删除的知识库主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteAiKnowledgeByIds(String[] ids) {
        List<String> targetIds = Arrays.asList(ids);
        baseMapper.deleteBatchIds(targetIds);
        // 删除文件表数据
        aiKnowledgeFileService.deleteBatch(targetIds);
        // 删除分片表数据
        aiKnowledgeFragmentationService.deleteBatch(targetIds);
    }
}
